package com.hjj.web.controller;


import com.hjj.web.entity.User;
import com.hjj.web.service.IUserService;
import com.hjj.web.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hejiajun
 * @since 2020-12-04
 */
@Slf4j
@Controller
public class UserLoginController {

    @Autowired
    IUserService userService;

    /**
     * 登录 跳转
     *
     * @param userName    用户名
     * @param pwd         密码
     * @param httpSession
     * @return
     */
    @PostMapping("/loginValidate")
    public String loginValidate(@RequestParam("userName") String userName, @RequestParam("password") String pwd, HttpSession httpSession) {
        if (StringUtils.isBlank(userName)) {
            return "login";
        }
        User user = userService.getUserByName(userName);
        if (user != null) {
            if (StringUtils.isNotBlank(user.getPassword()) && pwd.equals(user.getPassword())) {
                httpSession.setAttribute("userId", user.getUserId());
                return "chatroom";
            } else {
                return "fail";
            }
        }
        return "fail";
    }

    /**
     * 跳转 登录界面
     *
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 退出
     *
     * @param httpSession
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession httpSession) {
        return "login";
    }

}
